package com.app.micaihu.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


/**
 * Created by Guo on 2016/7/18.
 * 用户相关的数据库helper类
 */
public class UserSQLiteOpenHelper extends SQLiteOpenHelper {

    private Context context;
    public static final String DB_NAME = "micai_user.db";//数据库
//    public static final int DB_VERSION = 1;//版本号
//    public static final int DB_VERSION = 2;//版本号--UserTask 表中新增 5个字段
    public static final int DB_VERSION = 3;//版本号--UserInfor 表中新增 字段 签名
    public static final String USER_INFOR = "user_infor";//用户-表
    public static final String USER_TASK = "user_task";//用户任务-表
    public static final String USER_COLLECTION = "user_collection";//用户收藏-表


    public UserSQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        this.context = context;
    }

    public UserSQLiteOpenHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
//        int version = db.getVersion();
//        LogUtils.e("gzq","数据库当前版本version--"+version);
//        String sql = "create table if not exists "
//                + USER_INFOR
//                + "(_id integer primary key autoincrement"
//                + ",uid varchar(40),nickName varchar(20)"
//                + ",mobile varchar(20),password varchar(20)"
//                + ",headPic varchar(20),contribution varchar(20)"
//                + ",militaryTime varchar(20),jiamiphone varchar(20)"
//                + ",openId varchar(20),token varchar(20))";

        String sql = "create table if not exists "
                + USER_INFOR
                + "(_id integer primary key autoincrement"
                + ",uid varchar(40),nickName varchar(20)"
                + ",mobile varchar(20),password varchar(20)"
                + ",headPic varchar(20),contribution varchar(20)"
                + ",militaryTime varchar(20),jiamiphone varchar(20)"
                + ",openId varchar(20),token varchar(20)"
                + ",signature varchar(30));";
        try {
            db.execSQL(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }


       /* sql = "create table if not exists "
                + USER_TASK
                + "(_id integer primary key autoincrement"
                + ",uid varchar(40),level varchar(20)"
                + ",militaryExploit varchar(20),militarPay varchar(20)"
                + ",mobileBind varchar(20),dayPay varchar(20)"
                + ",dayExploit varchar(20),isLevelUp varchar(20)"
                + ",rankName varchar(20),nextRankName varchar(20)"
                + ",rankIcon varchar(20),nextRankIcon varchar(20)"
                + ",militaryExploitDiff varchar(20)"
                + ",firstLogin varchar(20),inviteNum varchar(20)"
                + ",wxAttion varchar(20),signIn varchar(20)"
                + ",commentNum varchar(20),shareNum varchar(20)"
                + ",militaryDiff varchar(20));";*/
        sql = "create table if not exists "
                + USER_TASK
                + "(_id integer primary key autoincrement"
                + ",uid varchar(40),level varchar(20)"
                + ",militaryExploit varchar(20),militarPay varchar(20)"
                + ",mobileBind varchar(20),dayPay varchar(20)"
                + ",dayExploit varchar(20),isLevelUp varchar(20)"
                + ",rankName varchar(20),nextRankName varchar(20)"
                + ",rankIcon varchar(20),nextRankIcon varchar(20)"
                + ",militaryExploitDiff varchar(20)"
                + ",firstLogin varchar(20),inviteNum varchar(20)"
                + ",wxAttion varchar(20),signIn varchar(20)"
                + ",commentNum varchar(20),shareNum varchar(20)"
                + ",militaryDiff varchar(20)"
                + ",rewardWx varchar(10)"
                + ",rewardMobile varchar(10)"
                + ",rewardSignSeven varchar(10)"
                + ",rewardSignThirty varchar(10)"
                + ",rewardInviteTen varchar(10));";
        try {
            db.execSQL(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }


        sql = "create table if not exists "
                + USER_COLLECTION
                + "(_uid varchar(40) primary key,json varchar(20))";
        try {
            db.execSQL(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //版本升级用
        //LogUtils.e("gzq","oldVersion=="+oldVersion+"---newVersion=="+newVersion);
        switch (oldVersion){
            case 1:
                version1_0_1(db);
            case 2:
                version2_0_3(db);
                break;

        }

    }

    /**
     * 203新增 userinfor表 的 签名
     * @param db
     */
    private void version2_0_3(SQLiteDatabase db) {

        try{
            db.beginTransactionNonExclusive();
            db.execSQL("ALTER TABLE " + USER_INFOR + " ADD COLUMN signature VARCHAR(30);");
            db.setTransactionSuccessful();

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            db.endTransaction();
        }



    }

    /**
     * version 1.0.1----任务表中增加5个奖励字段
     * @param db
     */
    private void version1_0_1(SQLiteDatabase db) {
        try{
            db.beginTransactionNonExclusive();
            db.execSQL("ALTER TABLE " + USER_TASK + " ADD COLUMN rewardWx VARCHAR(10);");
            db.execSQL("ALTER TABLE " + USER_TASK + " ADD COLUMN rewardMobile VARCHAR(10);");
            db.execSQL("ALTER TABLE " + USER_TASK + " ADD COLUMN rewardSignSeven VARCHAR(10);");
            db.execSQL("ALTER TABLE " + USER_TASK + " ADD COLUMN rewardSignThirty VARCHAR(10);");
            db.execSQL("ALTER TABLE " + USER_TASK + " ADD COLUMN rewardInviteTen VARCHAR(10);");
            db.setTransactionSuccessful();

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            db.endTransaction();
        }
    }


    private boolean checkTableExist(SQLiteDatabase db, String tableName, String columnName) {
        boolean flag = false;
        Cursor cursor = null;
        try {
            //查询一行
            cursor = db.rawQuery("SELECT * FROM " + tableName + " LIMIT 0"
                    , null);
            int columnIndex = cursor.getColumnIndex(columnName);
//            flag = cursor != null && cursor.getColumnIndex(columnName) != -1;
            flag = cursor != null && cursor.getColumnIndex(columnName) == -1;
        } catch (Exception e) {
            //Log.e("","checkColumnExists1..." + e.getMessage()) ;
            e.printStackTrace();
        } finally {
            if (null != cursor && !cursor.isClosed()) {
                cursor.close();
            }
        }

        return flag;

    }

}
